package com.sp.scmdb.jobImpl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import com.sp.scmdb.entity.Alarm;
import com.sp.scmdb.entity.AlarmType;
import com.sp.scmdb.entity.Area;
import com.sp.scmdb.entity.AreaData;
import com.sp.scmdb.entity.Employee;
import com.sp.scmdb.job.DealWithAlarmJob;
import com.sp.scmdb.service.ParseDataService;
import com.sp.scmdb.utils.GroupList;

public class DealWithAlarmJobImpl implements DealWithAlarmJob {

	private static Log log = LogFactory.getLog(DealWithAlarmJobImpl.class);

	private ParseDataService parseDataService;

	@Autowired
	public void setParseDataService(ParseDataService parseDataService) {
		this.parseDataService = parseDataService;
	}

	@Override
	public void doAlarm() {
		List<AlarmType> alarmTypeList = parseDataService.alarmType();
		Map<Integer, AlarmType> mapAlarmType = GroupList.groupAlarmType(alarmTypeList);
		if (MapUtils.isNotEmpty(mapAlarmType)) {
			List<Area> listArea = parseDataService.queryRegional();
			if (CollectionUtils.isNotEmpty(listArea)) {
				for (Area area : listArea) {
					List<AreaData> listAreData = parseDataService.findData("from AreaData where reID="
							+ area.getId());
					if (area.getType().equals("2")) {
						if (mapAlarmType.get(6) != null && mapAlarmType.get(6).getIsUse().equals("1")) {
							List<Employee> listEmployee = parseDataService
									.findEmp("from Employee where reID is not null and reID<>''");
							Map<Integer, Employee> mapEmployee = GroupList.groupEmployee(listEmployee);
							limiteAreaAlarm(listAreData, mapEmployee,area);
						}
					}
				}
			}
		}
		//移除数据
		parseDataService.removeLabelList();
	}

	public void limiteAreaAlarm(List<AreaData> listAreData, Map<Integer, Employee> mapEmployee, Area area) {
		try {
			for (AreaData areaData : listAreData) {
				boolean insertFlag = true;
				Alarm alarm = null;
				String empType = "";
				List<Alarm> alarms = parseDataService.alarmList(" and alarmType=6 and empID="
						+ areaData.getEmpID() + " and reID=" + areaData.getReID() + " and state=0");
				if (mapEmployee != null) {
					if (mapEmployee.get(areaData.getEmpID()) != null) {
						Employee employee = mapEmployee.get(areaData.getEmpID());
						empType = employee.getWorkType().getName();
						String empReID = "," + employee.getReID() + ",";
						String areaRelID = "," + String.valueOf(areaData.getReID()) + ",";
						if (empReID.indexOf(areaRelID) < 0) {
							insertFlag = true;
						} else {
							insertFlag = false;
						}
					}
				}
				if (insertFlag) {
					if (CollectionUtils.isEmpty(alarms)) {
						alarm = new Alarm();
						alarm.setEmpID(areaData.getEmpID());
						alarm.setReID(areaData.getReID());
						alarm.setAlarmType(6);
						alarm.setInTime(new DateTime());
						if(empType.equals("游客")){
							alarm.setRmk("游客"+"进入限制区域");
						}
						parseDataService.saveAlarm(alarm);
					}
				}
			}
		} catch (Exception e) {
			log.error("insert limiteAreaAlarm  error:" + e.getMessage());
		}
	}
}
